package com.hhss.qishi.controller.rabbitmq.original;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;

/**
 * @Auther: zcc
 * @Date: 19-7-18 18:40
 * @Description: rabbitMQ连接工具类
 */
public class ConnectionUtil {

    @Value("${rabbitmq.host}")
    private static String host;

    @Value("${rabbitmq.port}")
    private static int port;

    @Value("${rabbitmq.testhost}")
    private static String virtualHost;

    @Value("${rabbitmq.username}")
    private static String username;

    @Value("${rabbitmq.password}")
    private static String password;

    public static Connection getConnection() throws Exception {
        //定义连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        /**
         * 网络故障自动连接恢复
         */
        factory.setAutomaticRecoveryEnabled(true);

        //设置服务地址
        factory.setHost("localhost");
        //端口
        factory.setPort(5672);
        //设置账号信息，vhost、用户名、密码
        factory.setVirtualHost("testhost");
        factory.setUsername("baili");
        factory.setPassword("q15725734");
        // 通过工程获取连接
        Connection connection = factory.newConnection();
        return connection;
    }
}
